0% completed
Problem Statement
Given an array nums containing integers, return true if the given array is monotonic. Otherwise, return false.
An array is monotonic if it follows below conditions.
- An array
numsismonotone increasingif for alli <= j,nums[i] <= nums[j]. - An array nums is
monotone decreasingif for alli <= j,nums[i] >= nums[j].
Examples
-
Example 1:
- Input:
nums = [1, 5, 10, 10, 15, 16, 90] - Expected Output:
true - Justification: The array consistently ascends or remains equal, thus it is monotone increasing.
- Input:
-
Example 2:
- Input:
nums = [9, 4, 2, 2, 0] - Expected Output:
true - Justification: The array values either decrease or stay the same throughout, making it monotone decreasing.
- Input:
-
Example 3:
- Input:
nums = [7, 8, 7, 7] - Expected Output:
false - Justification: Array is not monotonic as
8is in between two7.
- Input:
Solution
To solve this problem, we start by recognizing that a monotonic array has one of two characteristics: it's either entirely non-decreasing or entirely non-increasing. This observation is crucial because it allows us to simplify our checks to just two conditions across the array's entirety. We believe this approach is effective because it directly leverages the definition of monotonic arrays, enabling us to verify the condition with a single pass through the array. By evaluating adjacent elements from start to finish, we can determine if the array deviates from either monotonic condition. This method is efficient and straightforward, making it an ideal solution.
In the first step, we'll assess if the array is non-decreasing by comparing each element to its successor. Similarly, we'll check if it's non-increasing. If either condition holds for the entire array, our function will return true; otherwise, false. This dual-path check ensures we correctly handle arrays that are constant or have equal elements throughout, aligning with our monotonic criteria.
Step-by-Step Algorithm
-
Initialize Two Flags: Start by declaring two boolean variables,
isNonDecreasingandisNonIncreasing, and set them both totrue. These flags will help us track whether the array is non-decreasing or non-increasing, respectively. -
Iterate Through the Array: Loop through the array from the first element to the second-last element. We compare each element with its next element to determine the array's trend. This is because we need at least two elements to compare and determine a trend.
-
Check Non-Decreasing Trend: During each iteration, compare the current element with the next element. If the current element is greater than the next element, set
isNonDecreasingtofalse. This indicates that the array cannot be non-decreasing because we found at least one instance where a later element is smaller than its predecessor. -
Check Non-Increasing Trend: Similarly, if the current element is less than the next element, set
isNonIncreasingtofalse. This step checks for a non-increasing trend. Finding a later element larger than its predecessor means the array cannot be non-increasing. -
Determine Monotonicity: After completing the loop, check the flags. If either
isNonDecreasingorisNonIncreasingis stilltrue, the array is monotonic. Returntruein this case. If both flags arefalse, the array is not monotonic, so returnfalse.
Algorithm Walkthrough
Let's consider the input: nums = [1, 5, 10, 10, 15, 16, 90]
-
Initialize Flags:
isNonDecreasing = trueisNonIncreasing = true
-
First Iteration (1 -> 5):
- Compare 1 and 5: Since 1 < 5, the array might be non-decreasing. Change
isNonIncreasingtofalse.
- Compare 1 and 5: Since 1 < 5, the array might be non-decreasing. Change
-
Second Iteration (5 -> 10):
- Compare 5 and 10: Since 5 < 10, still possibly non-decreasing. No change to flags.
-
Third Iteration (10 -> 10):
- Compare 10 and 10: Equal elements do not affect monotonicity. No change to flags.
-
Fourth Iteration (10 -> 15):
- Compare 10 and 15: Since 10 < 15, the array continues to potentially be non-decreasing. No change to flags.
-
Fifth Iteration (15 -> 16):
- Compare 15 and 16: 15 < 16 supports non-decreasing trend. No change to flags.
-
Sixth Iteration (16 -> 90):
- Compare 16 and 90: 16 < 90 continues to support a non-decreasing trend. No change to flags.
-
Determine Monotonicity:
- After the final iteration,
isNonDecreasingisfalse, andisNonIncreasingremainstrue. - Since
isNonIncreasingistrue, the array[1, 5, 10, 10, 15, 16, 90]is determined to be monotonic.
- After the final iteration,
Code
Complexity Analysis
Time Complexity
The time complexity of the algorithm is n is the length of the input array. This is because the algorithm iterates through the array exactly once to check if it is either non-decreasing or non-increasing.
Space Complexity
The space complexity complexity of the algorithm is
On this page
Problem Statement
Examples
Solution
Step-by-Step Algorithm
Algorithm Walkthrough
Code
Complexity Analysis
Time Complexity
Space Complexity